package com.onpositive.mediawiki.deserializing;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

import com.onpositive.data.IPropertyConstants;
import com.onpositive.dataminer.core.DefaultUniverse;
import com.onpositive.knowledge.model.IThing;
import com.onpositive.knowledge.model.IType;
import com.onpositive.knowledge.model.IUniverse;
import com.onpositive.knowledge.model.values.DescribedValue;
import com.onpositive.knowledge.model.values.IHasValue;
import com.onpositive.semantic.model.api.property.IProperty;
import com.onpositive.semantic.model.api.realm.IRealm;
import com.onpositive.semantic.model.api.realm.Realm;
import com.onpositive.semantic.model.ui.generic.IKnownsImageObject;
import com.onpositive.units.ParsedLinkArray;
import com.onpositive.units.TextContent;
import com.onpositive.wikistorage.PictureManager;

public class StoredThing implements IThing, IType, IKnownsImageObject {

	String name ;
	SerializedUniverse universe ;
	
	public StoredThing(String name, SerializedUniverse universe) {
		super();
		this.name = name;
		this.universe = universe;
	}

	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return name;
	}

	@Override
	public IRealm<IType> getTypes() {
		
		Realm<IType> result = new Realm<IType>() ;
		
		IProperty prop = universe.getProperty( this.name, IPropertyConstants.PARENT_CATEGORIES_PROPERTY_NAME ) ;
		
		if( prop == null )
			return result ;
		
		Object values = prop.getValue( this ) ;
		
		if( values instanceof Collection<?> )
			for( Object obj : (Collection<?>)values )
				processValue( result, obj ) ;
			
		else
			processValue( result, values ) ;
					
		return result ;
	}
	
	private void processValue( Realm<IType> result , Object value )
	{
		if( !(value instanceof ParsedLinkArray) )
			return;
		
		Object value0 = ((ParsedLinkArray) value ).value() ;
		
		if( !(value0 instanceof ArrayList<?> ) )
			return;
		
		ArrayList<?> list = (ArrayList<?>) value0 ;
		
		for( Object obj : list )
		{
			if( !(obj instanceof String) )
				continue ;
			
			result.add( new StoredThing( (String) obj, universe ) );
		}
	}
	
	public String toString(){
		return name ;
	}

	@Override
	public String getImageID() {
//		Collection<String> children = parentUniverse.getChildren(name);
//		if (children == null || children.isEmpty()) {
//			return "com.onpositive.mediawiki.crawler.image5";
//		}
//		return "com.onpositive.mediawiki.crawler.category";
		return null ;
	}

	@Override
	public IRealm<IType> getSuperTypes() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((universe == null) ? 0 : universe.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		StoredThing other = (StoredThing) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (universe == null) {
			if (other.universe != null)
				return false;
		} else if (!universe.equals(other.universe))
			return false;
		return true;
	}
	
	public Object getLongDefaultImage() {
		Object defaultImage = getDefaultImage();
		if (defaultImage != null) {
			PictureManager instance = PictureManager.getInstance( CollectionDeserializer.getPicturePath( universe.getBinaryPath() ) );			
			boolean checkPicturePresence = instance.checkPicturePresence(
					defaultImage.toString(), name);
			if (!checkPicturePresence) {
				instance.grabPictureFromNet(defaultImage.toString(), name);
			}
			return instance.convertToLocalPath(defaultImage.toString(), name);

		}
		return null;
	}
	
	public Object getDefaultImage() {
		IProperty property = DefaultUniverse.getInstance().getProperty(this,
				IPropertyConstants.EXTERNAL_DEFAULT_PICTURE_PROPERTY_NAME);
		if (property != null) {
			Object value = property.getValue(this);

			if (value != null) {
				String rootPath = CollectionDeserializer.getPicturePath( universe.getBinaryPath() );
				File fl=new File(rootPath);
				if (value.toString().startsWith(fl.getAbsolutePath())) {
					value = value.toString().substring(
							(fl.getAbsolutePath().length()+"pictures".length()+2+name.length()+1));
				}
				return value;
			}
			return value;
		}
		return null;
	}
	
	Collection<IHasValue> getTextContent() {
		IProperty prop = DefaultUniverse.getInstance().getProperty(this, IPropertyConstants.TEXT_CONTENT_PROPERTY_NAME );
		Object o1 = prop.getValue( this ) ;
		if( ! ( o1 instanceof DescribedValue ) )
			return null ;
		
		DescribedValue dval = (DescribedValue) o1 ;
		
		Object o2 = dval.value() ;
		
		if( !(o2 instanceof TextContent) )
			return null ;
		
		TextContent tc = (TextContent) o2 ;
		
		Collection<IHasValue> col = (Collection<IHasValue>) tc.value() ;
		return col ;		
	}

}
